package com.wrial.seckill.controller;
/*
 * @Author  Wrial
 * @Date Created in 15:43 2020/4/6
 * @Description
 */

import com.wrial.seckill.error.CustomizeErrorCode;
import com.wrial.seckill.error.CustomizeException;
import com.wrial.seckill.response.CommonReturnType;
import com.wrial.seckill.service.OrderService;
import com.wrial.seckill.service.model.OrderModel;
import com.wrial.seckill.service.model.UserModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller("order")
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private HttpServletRequest httpServletRequest;

    //封装下单请求
    @RequestMapping(value = "/createorder", method = {RequestMethod.POST}, consumes = {contentTypeFormed})
    @ResponseBody
    public CommonReturnType createOrder(@RequestParam(name = "itemId") Integer itemId,
                                        @RequestParam(name = "amount") Integer amount,
                                        @RequestParam(name = "promoId", required = false) Integer promoId) {

        Boolean isLogin = (Boolean) httpServletRequest.getSession().getAttribute("IS_LOGIN");
        if (isLogin == null || !isLogin.booleanValue()) {
            throw new CustomizeException(CustomizeErrorCode.USER_NOT_LOGIN.setMessage("用户还未登陆，不能下单"));
        }

        //获取用户的登陆信息
        UserModel userModel = (UserModel) httpServletRequest.getSession().getAttribute("LOGIN_USER");

        OrderModel orderModel = orderService.createOrder(userModel.getId(), itemId, promoId, amount);

        return CommonReturnType.create(null);
    }


}
